<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class PicscrollController extends Controller
{
    public function index()
    {
    	$list = DB::table('picscroll')->get();
    	return view('admin.picscroll.index',['list'=>$list]);
    }
    public function destroy($id)
    {
    	DB::table('picscroll')->where('id',$id)->delete();
    	return redirect('admin/picscroll');
    }
    public function create()
    {
    	return view('admin.picscroll.add');
    }
    public function store(Request $request)
    {
    	if ($request->hasFile('pic')) {
            if ($request->file('pic')->isValid()) {
                $file = $request->file('pic');
                // dd($file);
                $ext = $file->getClientOriginalExtension();
                $picname = time().rand(1000,9999).'.'.$ext;
                // 保存图片
                $file->move('./picscroll/',$picname);
                //自定义类进行图片缩放处理
                // $img = new Image();
                // $im = $img->open('./uploads/'.$picname);
                // $im->thumb(160,110)->save('./uploadss/'.$picname);
                $data = array('pic'=>$picname);
                $m = DB::table('picscroll')->insertGetId($data);
                if ($m>0) {
                	return redirect('admin/picscroll');
                }else{
        			return back();
        		}
            }
        }
    }
    public function edit($id)
    {
    	$list = DB::table('picscroll')->where('id',$id)->first();
    	return view('admin.picscroll.edit',['list'=>$list]);
    }
    public function update($id,Request $request)
    {
    	if ($request->file('pic')->isValid()) {
            $file = $request->file('pic');
            // dd($file);
            $ext = $file->getClientOriginalExtension();
            $picname = time().rand(1000,9999).'.'.$ext;
            // 保存图片
            $file->move('./picscroll/',$picname);
            //自定义类进行图片缩放处理
            // $img = new Image();
            // $im = $img->open('./uploads/'.$picname);
            // $im->thumb(160,110)->save('./uploadss/'.$picname);
            $data = array('pic'=>$picname);
             $m = DB::table('picscroll')->where('id',$id)->update($data);
             if ($m>0) {
              	return redirect('admin/picscroll');
             }else{
		      	return redirect('admin/picscroll');
		    }
    	}
    }
}
